package cn.itrip.service.impl;

import cn.itrip.beans.vo.hotel.ItripHotelVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.common.PropertiesUtils;
import cn.itrip.dao.BaseQuery;
import cn.itrip.service.SearchHotelService;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("searchHotelService")
public class SearchHotelServiceImpl implements SearchHotelService {

    public static String URL = PropertiesUtils.get("database.properties","baseUrl");
    private BaseQuery<ItripHotelVO> itripHotelVOBaseQuery = new BaseQuery<>(URL);

    @Override
    public Page<ItripHotelVO> searchItripHotelPage(SearchHotelVO vo, Integer pageNo, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        StringBuilder qQuery = new StringBuilder();
        if(EmptyUtils.isNotEmpty(vo)){
            qQuery.append(" destination:"+vo.getDestination());
            if(EmptyUtils.isNotEmpty(vo.getKeywords())){
                qQuery.append(" AND keyword:"+vo.getKeywords());
            }
            if(EmptyUtils.isNotEmpty(qQuery.toString())){
                query.setQuery(qQuery.toString());
            }
            if(EmptyUtils.isNotEmpty(vo.getHotelLevel())){
                query.addFilterQuery("hotelLevel:"+vo.getHotelLevel()+"");
            }
            if(EmptyUtils.isNotEmpty(vo.getFeatureIds())){
                StringBuilder buffer = new StringBuilder("(");
                int flag = 0;
                String featureIdArray[] = vo.getFeatureIds().split(",");
                for (String featureId : featureIdArray){
                    if(flag == 0){
                        buffer.append(" featureIds:"+"*,"+featureId+",*");
                    }else{
                        buffer.append(" OR featureIds:"+"*,"+featureId+",*");
                    }
                    flag++;
                }
                buffer.append(")");
                query.addFilterQuery(buffer.toString());
            }
            if(EmptyUtils.isNotEmpty(vo.getTradeAreaIds())){
                StringBuilder buffer = new StringBuilder("(");
                int flag = 0;
                String tradeAreaIdArray[] = vo.getTradeAreaIds().split(",");
                for (String tradeAreaId : tradeAreaIdArray){
                    if(flag == 0){
                        buffer.append(" tradingAreaIds:"+"*,"+tradeAreaId+",*");
                    }else{
                        buffer.append(" OR tradingAreaIds:"+"*,"+tradeAreaId+",*");
                    }
                    flag++;
                }
                buffer.append(")");
                query.addFilterQuery(buffer.toString());
            }
            if(EmptyUtils.isNotEmpty(vo.getMaxPrice())){
                query.addFilterQuery("minPrice:"+"[* TO "+vo.getMaxPrice()+"]");
            }
            if(EmptyUtils.isNotEmpty(vo.getMinPrice())){
                query.addFilterQuery("minPrice:"+"[* TO "+vo.getMinPrice()+"]");
            }
            if(EmptyUtils.isNotEmpty(vo.getAscSort())){
                query.addSort(vo.getAscSort(),SolrQuery.ORDER.asc);
            }
            if(EmptyUtils.isNotEmpty(vo.getDescSort())){
                query.addSort(vo.getDescSort(),SolrQuery.ORDER.desc);
            }
        }
        Page<ItripHotelVO> page = itripHotelVOBaseQuery.queryPage(query,pageNo,pageSize,ItripHotelVO.class);
        return page;
    }

    @Override
    public List<ItripHotelVO> searchItripHotelListByHotCity(Integer cityId, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        if(EmptyUtils.isNotEmpty(cityId)){
            query.addFilterQuery("cityId:"+cityId);
        }else{
            return null;
        }
        List<ItripHotelVO> hotelVOList = itripHotelVOBaseQuery.queryList(query,pageSize,ItripHotelVO.class);
        return hotelVOList;
    }
}
